#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
    pub val: i32,
    pub left: Option<Rc<RefCell<TreeNode>>>,
    pub right: Option<Rc<RefCell<TreeNode>>>,
}

impl TreeNode {
    #[inline]
    pub fn new(val: i32) -> Self {
        TreeNode {
            val,
            left: None,
            right: None,
        }
    }
}
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
    pub fn diameter_of_binary_tree(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
        let (_,ans)=Solution::dfs(root);
        ans
    }
    fn dfs(root :Option<Rc<RefCell<TreeNode>>>)->(i32,i32){
        if root.is_none(){
            return (0,0);
        }
        let root=root.unwrap().replace(TreeNode::new(0));
        let TreeNode{val:_,left,right}=root;
        let (left_height,l_max)=Solution::dfs(left);
        let (right_height,r_max)=Solution::dfs(right);
        let max=(left_height+right_height+1).max(l_max).max(r_max);
        return (left_height.max(right_height)+1,max);
    }
}
